package com.timschimandle.getitdone;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TaskDbAccess {
	
	private TaskDbHelper db;
	
	public TaskDbAccess(Context c) {
		this.db = new TaskDbHelper(c);
	}
	
	/*CRUD operations: Create Read Update Delete
	*tbl_people operations
	*c*/
	public boolean addPerson(Person p) {
		boolean success = true;
		
		//set up the content values to add a person to the database
		ContentValues values = new ContentValues();
		values.put(db.getFirstName(), p.getFirstName());
		values.put(db.getLastName(), p.getLastName());
		values.put(db.getEmail(), p.getEmail());
		values.put(db.getCellPhone(), p.getPhoneNumber());
		
		//open the db
		SQLiteDatabase d = db.getWritableDatabase();
		
		//try to write to the db, assume it works, if not, adjust the boolean value to return to the user
		try {
			d.insertOrThrow(db.getTblPeople(), null, values);
		} catch (SQLException e) {
			//set the boolean to false since it did not complete
			success = false;
			//print the stack trace
			e.printStackTrace();
		}
		
		d.close();
		
		return success;
	}
	
	//read
	public Person getPerson(Long id) {
		
		Person p;
		
		SQLiteDatabase d = db.getReadableDatabase();
		
		Cursor cursor = d.query(db.getTblPeople(), null, db.getId() + " = ?", new String[] {id.toString()}, null, null, null, null);
		
		if(cursor != null) {
			cursor.moveToFirst();
			p = new Person(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
		} else {
			p = null;
		}
		
		d.close();
		//if a is null then the method has failed
		return p;
		
	}
	
	public ArrayList<Person> getAllPeople() {
		
		ArrayList<Person> pl = new ArrayList<Person>();
		SQLiteDatabase d = db.getReadableDatabase();
		Cursor cu = d.query(db.getTblPeople(), null, null, null, null, null, null);
		//loop through cursor
		cu.moveToFirst();
		while (cu.isAfterLast() == false) {
			Person t = getPerson(cu.getLong(0));
			pl.add(t);
			cu.moveToNext();
		}
		cu.close();
		db.close();
		return pl;
	}
	//update
	public int updatePerson(Person p) {
		
		
		SQLiteDatabase d = db.getWritableDatabase();
		
		ContentValues values =  new ContentValues();
		values.put(db.getFirstName(), p.getFirstName());
		values.put(db.getLastName(), p.getLastName());
		values.put(db.getEmail(), p.getEmail());
		values.put(db.getCellPhone(), p.getPhoneNumber());
		
		int rowsAffected = d.update(db.getTblPeople(), values, db.getId() + " = ?", new String[] {p.getID().toString()});
		
		d.close();
		//update the record
		return rowsAffected;
		
	}
	
	//delete
	public void deletePerson(Long id) {
		SQLiteDatabase d = db.getWritableDatabase();
	    d.delete(db.getTblPeople(), db.getId() + " = ?",
	            new String[] {id.toString()});
	    d.close();
	}
	
	/*CRUD operations: Create Read Update Delete
	*tbl_task operations
	*c*/
	public boolean addTask(Task t) {
		
		boolean success = true;
		
		//set up the content values to add a task to the database
		ContentValues values = new ContentValues();
		values.put(db.getTitle(), t.getTitle());
		values.put(db.getDay(), t.getDatePart(0));
		values.put(db.getMonth(), t.getDatePart(1));
		values.put(db.getYear(), t.getDatePart(2));
		values.put(db.getPerson(), t.getPerson());
		values.put(db.getActive(), t.getActive());
		
		//insert the row
		SQLiteDatabase d = db.getWritableDatabase();
		
		//try to write to the db, assume it works, if not, adjust the boolean value to return to the user
		try {
			d.insertOrThrow(db.getTblTasks(), null, values);
		} catch (SQLException e) {
			//set the boolean to false
			success = false;
			//print the stack trace
			e.printStackTrace();
		}
		
		d.close();
		return success;
	}
	
	//read
	public Task getTask(Long id) {
		
		Task t;
		
		SQLiteDatabase d = db.getReadableDatabase();
		
		Cursor cursor = d.query(db.getTblTasks(), null, db.getId() + "=?",
	            new String[] { id.toString() }, null, null, null, null);
		
		if(cursor != null) {
			cursor.moveToFirst();
			t = new Task(cursor.getLong(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), cursor.getInt(4), cursor.getLong(5), cursor.getInt(6));
			//set the id value of the assignee
		} else {
			t = null;
		}
		
		d.close();
		//if a is null then the method has failed
		return t;
	}
	
	public ArrayList<Task> getAllTasks() {
		
		ArrayList<Task> tl = new ArrayList<Task>();
		SQLiteDatabase d = db.getReadableDatabase();
		Cursor cu = d.query(db.getTblTasks(), null, null, null, null, null, null);
		//loop through cursor
		cu.moveToFirst();
		while (cu.isAfterLast() == false) {
			Task t = getTask(cu.getLong(0));
			tl.add(t);
			cu.moveToNext();
		}
		cu.close();
		db.close();
		return tl;
	}
	
	//update, needs an id in the task object
	public int updateTask(Task t) {
		
		//i should probably add a check here to see if the task has an id
		SQLiteDatabase d = db.getWritableDatabase();
		
		ContentValues values =  new ContentValues();
		values.put(db.getTitle(), t.getTitle());
		values.put(db.getDay(), t.getDatePart(0));
		values.put(db.getMonth(), t.getDatePart(1));
		values.put(db.getYear(), t.getDatePart(2));
		values.put(db.getPerson(), t.getPerson());
		values.put(db.getActive(), t.getActive());
		
		int rowsAffected = d.update(db.getTblTasks(), values, db.getId() + " = ?", new String[] {t.getId().toString()});
		d.close();
		
		return rowsAffected;
	}
	
	//delete, needs an id in the task object
	public void deleteTask(Long id) {
		SQLiteDatabase d = db.getWritableDatabase();
	    d.delete(db.getTblTasks(), db.getId() + " = ?", new String[] { id.toString() });
	    d.close();
	}
	
}
